using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using PHSRAG.Insight21.Reports;
using System.Collections;
using System.Text;

namespace PHSRAG.Insight21.Agreements.Reports
{

    /// <remarks>Class for generating Financial Summary report</remarks>
    public class sqlFinancialSummaryReport:sqlReport
    {
        /// <summary>
        /// constant report name
        /// </summary>
        private const string cReportName = "Agmt_FinancialSummary";
        private const string cReportTitle = "Financial Summary";
        private const string agmtUrl = "http://localhost/Insight_220/Agreements/AgreementSummary.aspx";
        private const string SearchCriteriaVersionKey = "SearchCriteriaVersion_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string CostOptionKey = "CostOption_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string SortColumnKey = "SortColumn_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string SortDirectionKey = "SortAscending_12B90EAE-E704-444a-8733-800F3F39866C";

        private bool isAgmtRpt = false;

        public sqlFinancialSummaryReport(string pageUrl):base(pageUrl)
        {
            doHashtable = true;
            //doCustomExcelReport = true;

            //pUrl.bLoadParameter = false;
            //pUser.bLoadParameter = false;
            pDataSource.bLoadParameter = false;
            pRecordCount.bLoadParameter = false;
            if (pageUrl == agmtUrl)
                isAgmtRpt = true;
        }

        public override bool doCustomExcelReport
        {
            get
            {
                Agreements.FinancialSummaries financialSummaries = FinancialSummaries;
                return financialSummaries.Count>5000?false:true;
            }
            set
            {
                base.doCustomExcelReport = value;
            }
        }

        public override string ReportName
        {
            get
            {
                return cReportName;
            }
         }

  
        /// <summary>
        /// over ride parent method
        /// </summary>
        public override Hashtable setParameterHTable(ref string rptName)
        {
            rptName =rptFormat=="Excel"?ReportName+"Excel":ReportName;
            // set report data source
            //Agreements.FinancialSummaries financialSummaries = FinancialSummaries;
            //reportSource = financialSummaries.GetFundInfo(-1, financialSummaries.Count);

            // set report title
            reportTitle = isAgmtRpt?sqlAgreementSummaryReport.cReportTitle:cReportTitle;
           
            // set filter criteria
            reportCriteria = AddRefreshDatesToCriteria();

            string userid = userContext.DomainUserId.ToString();
            string usersessionid = Session.SessionID;
            bool includeIndirectCosts = IncludeIndirectCosts;
            htParameter.Add("domainUserId", userid);
            htParameter.Add("sessionid", usersessionid);
            htParameter.Add("includeIndirect", Convert.ToString(includeIndirectCosts));
            return base.setParameterHTable(ref rptName);

        }

        /// <summary>
        /// Get the Agreements (summary of agreements) that the logged-in user has access to. The list of agreements is
        /// is maintained by the Agreements business object within its data container. The Agreements object is cached.
        /// </summary>
        private Agreements.FinancialSummaries FinancialSummaries
        {
            get
            {
                Agreements.FinancialSummaries financialSummaries =
                    Policy.ResultsCache.GetObject(cachePolicy, Agreements.FinancialSummaries.CacheKey) as Agreements.FinancialSummaries;

                int searchVersion = searchCriteria.Version;

                if (financialSummaries == null || ((searchContext != null) && (searchVersion != SearchCriteriaVersion)))
                {
                    // When this page loads, we want the active funds to come back, but the 
                    // search criteria isn't setup until AgreementSearch loads, so set that
                    // one property here.
                    //if (searchVersion == 0)
                    //{
                    //    DropDownList ddl = (DropDownList)(expandingPanel.FindControl("agreementSearch").FindControl(Constants.FundStatusIDKey));
                    //    searchCriteria.Add(ddl.ID, ActiveFundStatus);
                    //    if (ddl.Attributes["Label"] != null)
                    //        searchCriteria.ControlLabels.Add(ddl.ID,
                    //            String.Format("{0}{1}{2}", ddl.Attributes["Label"], LabelValueSeparator,
                    //            ActiveFundStatusText));
                    //}

                    SearchCriteriaVersion = searchVersion;

                    Policy.ResultsCache.PurgeObject(cachePolicy);

                    financialSummaries = new Agreements.FinancialSummaries();
                    financialSummaries.Load(Utility.DataAccess.GetConnectionString(), searchCriteria,
                        glPostAsOfDate.ToShortDateString(), IncludeIndirectCosts, SortColumn, SortAscending, Session.SessionID);

                    Policy.ResultsCache.SetObject(cachePolicy, Agreements.FinancialSummaries.CacheKey, financialSummaries);
                }

                return financialSummaries;
            }
        }

        /// <summary>
        /// Gets/sets an integer value indicating the search criteria version. 
        /// The version number changes when the search criteria changes.  This signals the page to re-build its
        /// cache.
        /// </summary>
        /// <remarks>
        /// This and all properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the page cache under the key Keys.FinancialSummaryPageKey.
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        private int SearchCriteriaVersion
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                return (h == null || h[SearchCriteriaVersionKey] == null) ? 0 : (int)h[SearchCriteriaVersionKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[SearchCriteriaVersionKey] = value;
                pageCachePolicy.Cache(Keys.FinancialSummaryPageKey, h);
            }
        }

        /// <summary>
        /// Gets/sets the cost option which when set to true means total cost and when set to false means direct cost.
        /// </summary>
        /// <remarks>
        /// This and all properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the page cache under the key Keys.FinancialSummaryPageKey.
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        private bool IncludeIndirectCosts
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                return (h == null || h[CostOptionKey] == null) ? true : (bool)h[CostOptionKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[CostOptionKey] = value;
                pageCachePolicy.Cache(Keys.FinancialSummaryPageKey, h);
            }
        }

        /// <summary>
        /// Private boolean property used to get/set the base sorting column.
        /// </summary>
        /// <remarks>
        /// This and all properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the page cache under the key Keys.FinancialSummaryPageKey.
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        private string SortColumn
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) return "CurrentCashBalance";
                object sortColumn = h[SortColumnKey];
                return (sortColumn == null) ? "CurrentCashBalance" : (string)sortColumn;
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[SortColumnKey] = value;
                pageCachePolicy.Cache(Keys.FinancialSummaryPageKey, h);
            }
        }

        /// <summary>
        /// Private boolean property used to get/set if the sort order is ascending or descending.
        /// </summary>
        /// <remarks>
        /// This and all properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the page cache under the key Keys.FinancialSummaryPageKey.
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        private bool SortAscending
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) return true;
                object sortAscending = h[SortDirectionKey];
                return (sortAscending == null) ? true : (bool)sortAscending;
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[SortDirectionKey] = value;
                pageCachePolicy.Cache(Keys.FinancialSummaryPageKey, h);
            }
        }

    }

}